package com.xxx.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.dao.ProductDao;
import com.xxx.pojo.Collection;
import com.xxx.pojo.Product;
import com.xxx.service.ProductService;
import com.xxx.service.ex.ProductNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;


@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;

    //显示热门商品功能
    @Override
    public List<Product> getHotProducts() {
        LambdaQueryWrapper<Product> wrapper=new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Product::getPriority).last("limit 0,4");
        List<Product> result = productDao.selectList(wrapper);
        if(result.size()==0){
            return null;
        }
        for (Product product : result) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return result;
    }

    //获取单个商品信息功能
    @Override
    public Product getOneProduct(Integer id) {
        Product product = productDao.selectById(id);
        if(product==null){
            throw new ProductNotFoundException("商品信息未找到");
        }
        //无用的属性设为null
        product.setModifiedTime(null);
        product.setModifiedUser(null);
        product.setCreatedTime(null);
        product.setCreatedUser(null);
        product.setPriority(null);
        return product;
    }

    //根据条件获取商品列表 模糊查询 分页

    @Override
    public IPage getProductsByCondition(Integer currentPage,Integer pageSize,String condition) {
        IPage<Product> page=new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<Product> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(Product::getTitle,condition);
        IPage<Product> productIPage = productDao.selectPage(page, wrapper);
        return productIPage;
    }

    //显示新上架商品

    @Override
    public List<Product> getNewProducts() {
        LambdaQueryWrapper<Product> wrapper=new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Product::getCreatedTime).last("limit 0,4");
        List<Product> result = productDao.selectList(wrapper);
        if(result.size()==0){
            return null;
        }
        for (Product product : result) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return result;
    }
}
